package com.echo.register.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.echo.register.client.cmn.DictFeignClient;
import com.echo.register.enums.DictEnum;
import com.echo.register.hosp.repository.HospitalRepository;
import com.echo.register.hosp.service.HospitalService;
import com.echo.register.model.hosp.Hospital;
import com.echo.register.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HospitalServiceImpl
 * @Package: com.echo.register.hosp.service.impl
 * @Description: 医院管理服务端实现
 * @Author 谢同学
 * @Create 2023/12/19 11:27
 * @Version 1.0
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;


    /**
     * 上传医院信息
     *
     * @param paramMap 对象
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        // 数据转对象：先转json数据再转对象
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        // 判断医院信息是否已经存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (targetHospital == null) {// 如果不存在新增
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setIsDeleted(0);
        }else {
            // 存在修改
            hospital.setId(targetHospital.getId()); // id不变
            hospital.setStatus(targetHospital.getStatus());// 状态不变
            hospital.setCreateTime(targetHospital.getCreateTime()); // 创建时间不变
            hospital.setIsDeleted(0);
        }
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 查询医院
     *
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);

        // 处理数据
        this.packHospital(hospital);
        return hospital;
    }

    /**
     * 分页查询
     *
     * @param page            当前页码
     * @param limit           每页记录数
     * @param hospitalQueryVo 查询条件
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        hospital.setIsDeleted(0);
        //hospital.setStatus(1);
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(page - 1, limit, sort);

        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> hospitals = hospitalRepository.findAll(example, pageRequest);
        // 处理数据

        hospitals.getContent().forEach(this::packHospital);

        return hospitals;
    }

    /**
     * 更新上线状态
     *
     * @param id     医院编号
     * @param status 状态
     */
    @Override
    public void updateStatus(String id, Integer status) {
        // 查询数据库中原本的状态
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        // 更新数据库
        hospitalRepository.save(hospital);
    }

    /**
     * 根据医院编号获取医院名称接口
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null != hospital) {
            return hospital.getHosname();
        }
        return "";
    }

    /**
     * 根据医院名称模糊查询医院列表
     *
     * @param hosname 医院名称
     * @return 医院列表
     */
    @Override
    public List<Hospital> findHospitalByHosnameLike(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 预约挂号查询医院信息
     *
     * @param hoscode 医院编号
     * @return
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        // 医院详情
        Hospital hospital = this.getHospitalByHoscode(hoscode);
        HashMap<String, Object> result = new HashMap<>();
        result.put("hospital",hospital);
        //预约规则
        result.put("bookingRule",hospital.getBookingRule());
        // 避免重复返回
        hospital.setBookingRule(null);
        return result;
    }


    // 处理医院数据
    private void packHospital(Hospital hospital){
        // 医院等级
        String hosTypeString = null;
        if (DictEnum.HOSTYPE.getDictCode() != null && hospital != null && hospital.getHostype() != null) {
        //if (DictEnum.HOSTYPE.getDictCode() != null) {
            hosTypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        }
        // 省
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        // 市
        String city = dictFeignClient.getName(hospital.getCityCode());
        // 区
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        // 封装属性
        hospital.getParam().put("hosTypeString",hosTypeString);
        hospital.getParam().put("fullAddress",province+city+district+hospital.getAddress());
    }

}

